﻿using System;
using System.CodeDom;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Marmat.Template.WinForm
{

    public class ObjectConversionProperties
    {
        public ObjectConversion Conversion { get; set; }
        public string PrimaryName { get; set; }

        /// <summary>
        /// 处理添加的集合。
        /// </summary>
        /// <param name="type"></param>
        /// <param name="collection"></param>
        /// <param name="values"></param>
        public virtual void AdditionCollection(Type type, System.Collections.IEnumerable collection, List<object> values)
        {
            foreach (var item in collection)
            {
                var value = this.Conversion.Conversion(type, null, item);
                if (value != null)
                    values.Add(new object[] { 0, value });
            }
        }

        /// <summary>
        /// 处理变更的集合。
        /// </summary>
        /// <param name="type"></param>
        /// <param name="collection"></param>
        /// <param name="values"></param>
        public virtual void ChangeCollection(Type type, Dictionary<object, object> collection, List<object> values)
        {
            foreach (var item in collection)
            {
                Dictionary<string, object> result = this.Conversion.Conversion(type, item.Key, item.Value);
                if (result != null)
                {
                    values.Add(new object[] { 1, result });
                }
            }
        }

        /// <summary>
        /// 处理删除的集合。
        /// </summary>
        /// <param name="type"></param>
        /// <param name="collection"></param>
        /// <param name="values"></param>
        public virtual void DeleteCollection(Type type, System.Collections.IEnumerable collection, List<object> values)
        {
            foreach (var item in collection)
            {
                object primaryValue = this.Conversion.GetPrimaryValue(item);
                values.Add(new object[] { 2, primaryValue });
            }
        }
    }

    public class ObjectConversionProperties<T> : ObjectConversionProperties where T : class
    {

        /// <summary>
        /// 指定序列化的字段。
        /// </summary>
        /// <param name="expression"></param>
        public ObjectConversionProperties<T> Serialize(Expression<Func<T, object>> expression)
        {
            this.Conversion.Serialize(expression);
            return this;
        }

        /// <summary>
        /// 指定序列化的字段。
        /// </summary>
        /// <param name="memberBuilder"></param>
        public ObjectConversionProperties<T> SerializeMember(Func<MemberInfo, bool> memberBuilder)
        {
            this.Conversion.SerializeMember<T>(memberBuilder);
            return this;
        }

        /// <summary>
        /// 指定序列化的字段。
        /// </summary>
        /// <param name="memberBuilder"></param>
        public ObjectConversionProperties<T> SerializeMember(Func<System.Type, MemberInfo, bool> memberBuilder)
        {
            this.Conversion.SerializeMember<T>(memberBuilder);
            return this;
        }
    }
}
